import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ncov_2019/commom/user.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:ncov_2019/commom/application.dart';
import 'package:ncov_2019/commom/user.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:ncov_2019/event/login_event.dart';
import 'package:ncov_2019/http/api_service.dart';
import 'package:ncov_2019/http/UserModel.dart';
import 'package:ncov_2019/util/bubble_indication_painter.dart';
import 'package:ncov_2019/util/theme_util.dart';
import 'package:ncov_2019/commom/user.dart';
import 'package:ncov_2019/config/api.dart';
import 'package:ncov_2019/http/dio_manager.dart';
import 'package:ncov_2019/pages/login/login_page.dart';
import 'package:share/share.dart';
import 'package:ncov_2019/mqtt/Mqtt.dart';

class DrawerPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new DrawerPageState();
  }
}

class DrawerPageState extends State<DrawerPage> {
  bool isLogin = false;
  String username = "未登录";

  @override
  void initState() {
    super.initState();
    this.registerLoginEvent();
    if (null != User.singleton.userName) {
      isLogin = true;
      username = User.singleton.userName;
    }
  }

  void registerLoginEvent() {
    Application.eventBus.on<LoginEvent>().listen((event) {
      changeUI();
    });
  }

  changeUI() async {
    setState(() {
      isLogin = true;
      username = User.singleton.userName;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white
        ),
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            UserAccountsDrawerHeader(
              accountName: InkWell(
                child: Text(username,
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
                onTap: () {
                  if (!isLogin) {
                    Navigator.of(context)
                        .push(new MaterialPageRoute(builder: (context) {
                      return new LoginPage();
                    }));
                  }
                },
              ),
              currentAccountPicture: InkWell(
                child: CircleAvatar(
                  backgroundImage: AssetImage('images/head.jpg'),
                ),
                onTap: () {
                  if (!isLogin) {
                    Navigator.of(context)
                        .push(new MaterialPageRoute(builder: (context) {
                      return new LoginPage();
                    }));
                  }
                },
              ),
            ),
            ListTile(
              title: Text(
                '我的分享',
                textAlign: TextAlign.left,
              ),
              leading: Icon(Icons.collections, size: 22.0),
              onTap: () {
                if (isLogin) {
                  onCollectionClick();
                } else {
                  onLoginClick();
                }
              },
            ),
            ListTile(
              title: Text(
                '系统设置',
                textAlign: TextAlign.left,
              ),
              leading: Icon(Icons.web, size: 22.0),
              onTap: () {
                Navigator.of(context)
                    .push(new MaterialPageRoute(builder: (context) {
                  return new DrawerPage();
                }));
              },
            ),
//          ListTile(
//            title: Text(
//              '分享',
//              textAlign: TextAlign.left,
//            ),
//            leading: Icon(Icons.share, size: 22.0),
//            onTap: () {
//              Share.share(
//                  '给你推荐一个特别好玩的应用玩安卓客户端，点击下载：https://www.pgyer.com/haFL');
//            },
//          ),
//          ListTile(
//            title: Text(
//              '妹子图',
//              textAlign: TextAlign.left,
//            ),
//            leading: Icon(Icons.directions_bike, size: 22.0),
//            onTap: () {
//              Navigator.of(context)
//                  .push(new MaterialPageRoute(builder: (context) {
//                return new DrawerPage();
//              }));
//            },
//          ),
//          ListTile(
//            title: Text(
//              '关于作者',
//              textAlign: TextAlign.left,
//            ),
//            leading: Icon(Icons.info, size: 22.0),
//            onTap: () {
//              Navigator.of(context)
//                  .push(new MaterialPageRoute(builder: (context) {
//                return new DrawerPage();
//              }));
//            },
//          ),
            logoutWidget()
          ],
        ),
      ),
    );
  }

  Widget logoutWidget() {
    if (User.singleton.userName != null) {
      return ListTile(
        title: Text(
          '退出登录',
          textAlign: TextAlign.left,
        ),
        leading: Icon(Icons.power_settings_new, size: 22.0),
        onTap: () {
          User.singleton.clearUserInfor();
          setState(() {
            isLogin = false;
            username = "未登录";
          });
        },
      );
    } else {
      return SizedBox(
        height: 0,
      );
    }
  }

  void onCollectionClick() async {
    await Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return new DrawerPage();
    }));
  }

  void onLoginClick() async {
    await Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return new LoginPage();
    }));
  }

}
